/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Managers.Utilities;

import java.util.LinkedList;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * Messages container for request/response pairs associated failed requests.
 *
 * @author Angel Sanadinov
 */
public class ResponseMessagesContainer implements HttpSessionBindingListener
{
    private LinkedList<RequestResponseWrapper> messages; //messages list
    private int maxResponseMessages;

    /**
     * Constructs the messages container, initially empty.
     *
     * @param maxResponseMessages the maximum number of response messages to
     *        hold in the container
     */
    public ResponseMessagesContainer(int maxResponseMessages)
    {
        this.messages = new LinkedList<RequestResponseWrapper>();
        this.maxResponseMessages = maxResponseMessages;
    }

    /**
     * Retrieves the total number of messages contained in this object.
     *
     * @return the total number of messages
     */
    public int getNumberOfMessages()
    {
        return messages.size();
    }

    /**
     * Retrieves the number of newly added messages.
     *
     * @return the number of new messages
     */
    public int getNumberOfNewMessages()
    {
        int result = 0;

        for(RequestResponseWrapper message : messages)
        {
            if(message.isNewMessage())
                result++;
            else
                ;
        }

        return result;
    }

    /**
     * Marks all messages as read (old).
     */
    public void markAllMessagesAsRead()
    {
        for(RequestResponseWrapper message : messages)
            message.markAsRead();
    }

    /**
     * Retrieves the message at the specified index.
     *
     * @param index the position of the requested message in the list
     * @return the requested message
     */
    public RequestResponseWrapper getMessage(int index)
    {
        return messages.get(index);
    }

    /**
     * Inserts a new request/response wrapper (message) object at the end of the list. <br>
     *
     * <b>Note:</b> <i>If the maximum messages limit is reached, the oldest message
     * is removed.</i>
     *
     * @param wrapper the new wrapper object
     */
    public void put(RequestResponseWrapper wrapper)
    {
        if(messages.size() >= maxResponseMessages)
            messages.removeFirst();
        else
            ;

        messages.add(wrapper);
    }

    /**
     * Removes the message at the specified index.
     *
     * @param index the position of the message to be removed
     */
    public void remove(int index)
    {
        messages.remove(index);
    }

    /**
     * No action is taken when this object is bound to a sessions.
     *
     * @param event the event that identifies the session
     */
    @Override
    public void valueBound(HttpSessionBindingEvent event) {}

    /**
     * The messages in the container are cleared.
     *
     * @param event the event that identifies the session
     */
    @Override
    public void valueUnbound(HttpSessionBindingEvent event)
    {
        messages.clear();
        messages = null;
    }
}
